package com.work.rcj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.work.rcj.common.base.BaseController;
import com.work.rcj.common.exception.BusinessException;
import com.work.rcj.common.model.Response;
import com.work.rcj.common.model.page.PageResult;
import com.work.rcj.entity.OldPeople;
import com.work.rcj.entity.OldPeopleType;
import com.work.rcj.pojo.dto.OldPeopleDto;
import com.work.rcj.pojo.dto.OldPeopleTypeDto;
import com.work.rcj.service.OldPeopleService;
import com.work.rcj.service.OldPeopleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * 老年人分类管理
 *
 * @author rcj
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/oldPeopleType")
public class OldPeopleTypeController extends BaseController {

    @Autowired
    private OldPeopleTypeService oldPeopleTypeService;


    /**
     * 老年人分类列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public Response<PageResult<OldPeopleType>> oldPeopleList(OldPeopleTypeDto dto) {
        Page<OldPeopleType> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        QueryWrapper<OldPeopleType> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("deleted",1);
        if(!StringUtils.isEmpty(dto.getSearchKey())){
            queryWrapper.like("name",dto.getSearchKey());
        }
        IPage<OldPeopleType> list = oldPeopleTypeService.page(page, queryWrapper);
        return Response.success(getDataTable(list));
    }

    /**
     * 新增老年人分类
     *
     * @param req
     * @return
     */
    @PostMapping("/save")
    public Response save(@RequestBody OldPeopleType req) {
        QueryWrapper<OldPeopleType> wrapper = new QueryWrapper<>();
        wrapper.eq("name", req.getName());
        wrapper.eq("deleted", 0);
        OldPeopleType oldPeopleType = oldPeopleTypeService.getOne(wrapper);
        if (oldPeopleType != null) {
            throw new BusinessException("500001", "分类已存在");
        }
        oldPeopleTypeService.save(req);
        return Response.success();
    }

    /**
     * 修改老年人分类
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public Response update(@RequestBody OldPeopleType req) {
        QueryWrapper<OldPeopleType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", req.getName());
        queryWrapper.eq("deleted", 0);
        queryWrapper.ne("id", req.getId());
        OldPeopleType oldPeopleType = oldPeopleTypeService.getOne(queryWrapper);
        if (oldPeopleType != null) {
            throw new BusinessException("500001", "分类已存在");
        }

        UpdateWrapper<OldPeopleType> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", req.getId());
        oldPeopleTypeService.update(req, updateWrapper);
        return Response.success();
    }

    /**
     * 删除老年人分类
     *
     * @param req
     * @return
     */
    @PostMapping("/del")
    public Response del(@RequestBody OldPeopleType req) {
        OldPeopleType entity = new OldPeopleType();
        entity.setId(req.getId());
        entity.setDeleted("1");
        oldPeopleTypeService.updateById(entity);
        return Response.success();
    }

    /**
     * 老年人分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Response<OldPeopleType> detail(Long id) {
        OldPeopleType result = oldPeopleTypeService.getById(id);
        return Response.success(result);
    }


}

